﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;

namespace BYManageInfoSystem
{
    public partial class Frm_BYDeptInfo : DockContent
    {
        public static DGBY.BLL.BYDepartTab m_bllBYDepartTab = new DGBY.BLL.BYDepartTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectBYDepartTabID = 0;
        public static DGBY.BLL.BYDepCodeTab m_bllBYDepCodeTab = new DGBY.BLL.BYDepCodeTab();
        private DataSet m_dsBYDepCodeTab = new DataSet();
        private int m_nSelectBYDepCodeTabID = 0;

        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
       // ExcelBase eB = new ExcelBase();

        public Frm_BYDeptInfo()
        {
            InitializeComponent();
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewAllDeptList.DataSource = null;
            m_ds = m_bllBYDepartTab.GetAllList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewAllDeptList.DataSource = m_ds.Tables[0];
                dataGridViewAllDeptList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }

        private void dataGridViewAllDeptList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBYDepartTabID = this.dataGridViewAllDeptList.Rows[this.dataGridViewAllDeptList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBYDepartTabID = Convert.ToInt32(strBYDepartTabID, 10);
            cellclickBYDepartTab();
        }

        private void cellclickBYDepartTab()
        {
            DGBY.Model.BYDepartTab modelBYDepartTab = m_bllBYDepartTab.GetModel(m_nSelectBYDepartTabID);
            if (modelBYDepartTab != null)
            {
                this.textBoxDeptName.Text = modelBYDepartTab.DepartName;
                this.textBoxManager.Text = modelBYDepartTab.ManagerName;
                this.textBoxPhone.Text = modelBYDepartTab.Telephone;
                this.richTextBoxNote.Text = modelBYDepartTab.Note;
            }
            else
            {
                this.textBoxDeptName.Text = "";
                this.textBoxManager.Text = "";
                this.textBoxPhone.Text = "";
                this.richTextBoxNote.Text = "";
            }
        }

        private void dataGridViewAllDeptList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewAllDeptList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BYDepartTab modelBYDepartTab = new DGBY.Model.BYDepartTab();

                modelBYDepartTab.DepartName = this.textBoxDeptName.Text;
                modelBYDepartTab.ManagerName = this.textBoxManager.Text;
                modelBYDepartTab.Telephone = this.textBoxPhone.Text;
                modelBYDepartTab.Note = this.richTextBoxNote.Text;

                int nResult = m_bllBYDepartTab.Add(modelBYDepartTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", modelBYDepartTab.DepartName);
                    m_nSelectBYDepartTabID = m_bllBYDepartTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                InitSelfCompCombox();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
       
        }

        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllBYDepartTab.GetModel(m_nSelectBYDepartTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.BYDepartTab modelBYDepartTab = m_bllBYDepartTab.GetModel(m_nSelectBYDepartTabID);
                modelBYDepartTab.DepartName = this.textBoxDeptName.Text;
                modelBYDepartTab.ManagerName = this.textBoxManager.Text;
                modelBYDepartTab.Telephone = this.textBoxPhone.Text;
                modelBYDepartTab.Note = this.richTextBoxNote.Text;
              //  modelBYDepartTab.BYDepartTabID = m_nSelectBYDepartTabID;

                m_bllBYDepartTab.Update(modelBYDepartTab);

                Frm_Main.Log2DB(this.Text, "修改", modelBYDepartTab.DepartName);
                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllBYDepartTab.GetModel(m_nSelectBYDepartTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBYDepartTab.Delete(m_nSelectBYDepartTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickBYDepartTab();
            InitSelfCompCombox();
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewAllDeptList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "BYDeptInfo.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonPrintPreview_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewAllDeptList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "BYDeptInfo.xlt";
                    string outFile = "";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);
                    try
                    {
                        eB.DataTableToExcelAndPrintPreview(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("请先关闭当前excel文档!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private void buttonPrint_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewAllDeptList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "BYDeptInfo.xlt";
                    string outFile = "";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcelAndPrint(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void InitSelfCompCombox()
        {
            DataSet ds = m_bllBYDepartTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxDeptName.DataSource = ds.Tables[0];
                this.comboBoxDeptName.DisplayMember = "部门名称";
                this.comboBoxDeptName.ValueMember = "BYDepartTabID";
                this.comboBoxDeptName.SelectedIndex = 0;
            }
        }

        private void Frm_BYDeptInfo_Load(object sender, EventArgs e)
        {
            InitSelfCompCombox();
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
                this.buttonDeptCodeDel.Visible = true;
            }
        }

        private void ShowAllDeptCodeRecordInDataGridView()
        {
            dataGridViewDeptCodeList.DataSource = null;
            m_dsBYDepCodeTab = m_bllBYDepCodeTab.GetAllDepCodeInfo();
            if (m_dsBYDepCodeTab.Tables.Count > 0 && m_dsBYDepCodeTab.Tables[0].Rows.Count > 0)
            {
                dataGridViewDeptCodeList.DataSource = m_dsBYDepCodeTab.Tables[0];
                dataGridViewDeptCodeList.Columns[0].Visible = false;
            }

        }

        private void buttonDeptCodeShowAll_Click(object sender, EventArgs e)
        {
            ShowAllDeptCodeRecordInDataGridView();
        }

        private void dataGridViewDeptCodeList_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBYDepCodeTabID = this.dataGridViewDeptCodeList.Rows[this.dataGridViewDeptCodeList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBYDepCodeTabID = Convert.ToInt32(strBYDepCodeTabID, 10);
            cellBYDepCodeTabClick();
        }

        private void cellBYDepCodeTabClick()
        {
            DGBY.Model.BYDepCodeTab modelBYDepCodeTab = m_bllBYDepCodeTab.GetModel(m_nSelectBYDepCodeTabID);
            if (modelBYDepCodeTab != null)
            {
                this.textBoxDeptCode.Text = modelBYDepCodeTab.DepCodeName;
                if(modelBYDepCodeTab.BYDepartTabID != null)
                    this.comboBoxDeptName.SelectedValue = modelBYDepCodeTab.BYDepartTabID;
            }
            else
            {
                this.textBoxDeptCode.Text = "";
            }
        }

        private void dataGridViewDeptCodeList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewDeptCodeList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        private void buttonDeptCodeAdd_Click(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BYDepCodeTab modelBYDepCodeTab = new DGBY.Model.BYDepCodeTab();
                modelBYDepCodeTab.DepCodeName = this.textBoxDeptCode.Text;

                if (this.comboBoxDeptName.SelectedValue != null)
                    modelBYDepCodeTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDeptName.SelectedValue.ToString(), 10);


                int nResult = m_bllBYDepCodeTab.Add(modelBYDepCodeTab);

                //if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", this.comboBoxDeptName.Text + "(" + modelBYDepCodeTab.DepCodeName +")");
                    
                    m_nSelectBYDepCodeTabID = m_bllBYDepCodeTab.GetMaxId() - 1;
                    ShowAllDeptCodeRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
          
        }

        private void buttonDeptCodeMod_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllBYDepCodeTab.GetModel(m_nSelectBYDepCodeTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                DGBY.Model.BYDepCodeTab modelBYDepCodeTab = m_bllBYDepCodeTab.GetModel(m_nSelectBYDepCodeTabID);
                modelBYDepCodeTab.DepCodeName = this.textBoxDeptCode.Text;
              //  modelBYDepCodeTab.BYDepCodeTabID = m_nSelectBYDepCodeTabID;

                if (this.comboBoxDeptName.SelectedValue != null)
                    modelBYDepCodeTab.BYDepartTabID = Convert.ToInt32(this.comboBoxDeptName.SelectedValue.ToString(), 10);

                m_bllBYDepCodeTab.Update(modelBYDepCodeTab);

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxDeptName.Text + "(" + modelBYDepCodeTab.DepCodeName + ")");
                ShowAllDeptCodeRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonDeptCodeDel_Click(object sender, EventArgs e)
        {
            if (m_bllBYDepCodeTab.GetModel(m_nSelectBYDepCodeTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            m_bllBYDepCodeTab.Delete(m_nSelectBYDepCodeTabID);
            ShowAllDeptCodeRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellBYDepCodeTabClick();
        }

        private void buttonDeptCodeExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewDeptCodeList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "BYDeptCodeInfo.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcel(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonDeptCodePrintPreview_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewDeptCodeList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "BYDeptCodeInfo.xlt";
                    string outFile = "";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);
                    try
                    {
                        eB.DataTableToExcelAndPrintPreview(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {
                        MessageBox.Show("请先关闭当前excel文档!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void buttonDeptCodePrint_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)dataGridViewDeptCodeList.DataSource;
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    string templetFile = templetFilePath + "BYDeptCodeInfo.xlt";
                    string outFile = "";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.DataTableToExcelAndPrint(dt, 30, 6, 3, "页", 1);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("数据不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void Frm_BYDeptInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            //eB.Close();
        }


    }
}